package com.uuzuche.lib_zxing.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.uuzuche.lib_zxing.R;

/**
 * Initial the camera
 *
 * 默认的二维码扫描Activity
 */
public class CaptureActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        CaptureFragment captureFragment = new CaptureFragment();
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_zxing_container, captureFragment).commit();
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            if(result.equals("http://h5.binvshe.com/#/lottery/1")){
                    Log.e("jinlail","jinlail");
                    Intent resultIntent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                    bundle.putString(CodeUtils.RESULT_STRING, "http://h5.binvshe.com/#/lottery/1");
                    bundle.putInt(CodeUtils.TYPE, CodeUtils.RESULT_WEB);
                    resultIntent.putExtras(bundle);
                 CaptureActivity.this.setResult(RESULT_OK, resultIntent);
                    CaptureActivity.this.finish();

            }else if(!result.substring(0,21).equals("http://h5.binvshe.com")){
                Intent resultIntent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                bundle.putInt(CodeUtils.TYPE, CodeUtils.RESULT_GAME);
                resultIntent.putExtras(bundle);
                CaptureActivity.this.setResult(RESULT_OK, resultIntent);
                CaptureActivity.this.finish();
            }else{
                String[] type = result.split("/#/coin/");
                String substring = type[1].substring(0, 3);
                //送萌都币
                if(substring.equals("qrc")){
                    String[] qrcode = result.split("qrcode/");
                    String code = qrcode[1];
                    Intent resultIntent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                    bundle.putString(CodeUtils.RESULT_STRING, code);
                    bundle.putInt(CodeUtils.TYPE, CodeUtils.RESULT_GET);
                    resultIntent.putExtras(bundle);
                    CaptureActivity.this.setResult(RESULT_OK, resultIntent);
                    CaptureActivity.this.finish();
//                getCode(code);
                    Log.e("sssss","送");
                    //支付萌都币
                }else if(type[1].substring(0,3).equals("pay")){
                    String[] qrcode = result.split("pay/");
                    String code = qrcode[1];
                    Intent resultIntent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
                    bundle.putString(CodeUtils.RESULT_STRING, code);
                    bundle.putInt(CodeUtils.TYPE, CodeUtils.RESULT_PAY);
                    resultIntent.putExtras(bundle);
                    CaptureActivity.this.setResult(RESULT_OK, resultIntent);
                    CaptureActivity.this.finish();
                }else {


                }
            }

        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            CaptureActivity.this.setResult(RESULT_OK, resultIntent);
            CaptureActivity.this.finish();
        }
    };

//
}